'use strict';

module.exports = app => {
    return class StudyService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }


        /**
         * 创建练习
         * @param id
         * @returns {Promise<void>}
         */
        async add({ type, courseId }) {
            console.log(type);
            let url = this.app.urls('exerciseAdd');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data: {
                    type,
                    courseId
                }
            });
			// this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 作业报告
         * @param id
         * @param studentId
         * @param type
         * @param role
         * @returns {Promise<void>}
         */




        async report({ id }) {
            let url = this.app.urls('exerciseReport');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 题目详解
         * @param id
         * @param type
         * @returns {Promise<void>}
         */
        async detail({ id, type }) {
            let url = this.app.urls('exerciseDetail');
            if (type == 2){
                url = this.app.urls('exerciseErrorDetail');
            }

            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    studentTaskId: id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /**
         * 当个题目详解
         * @param id
         * @returns {Promise<void>}
         */
        async sigleDetail({ id }) {
            let url = this.app.urls('exerciseSingleDetail');
            if (type == 2){
                url = this.app.urls('errorDetail');
            }

            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /**
         * 专项巩固题目
         * @param studentId
         * @returns {Promise<void>}
         */
        async exercise({ id, pointId }) {
            let url = this.app.urls('exercise');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id,
                    pointId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async easyError({ id, pointId }) {
            let url = this.app.urls('exercise');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id,
                    pointId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        async personError({ id, pointId }) {
            let url = this.app.urls('exercise');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id,
                    pointId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async randomQuestion({ id, pointId }) {
            let url = this.app.urls('exercise');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id,
                    pointId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async historyExercise({ type, pageNo }) {
            let url = this.app.urls('historyExercise');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    type,
                    pageNo
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        async pointStatistics({ id, pointId }) {
            let url = this.app.urls('exercise');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id,
                    pointId
                }
            });
            // this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }





    }

};
